/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.MemberScore;
import com.boxin.ims.modules.wechat.dao.MemberScoreDao;

/**
 * 会员积分Service
 * @author Jakemanse
 * @version 2013-09-08
 */
@Component
@Transactional(readOnly = true)
public class MemberScoreService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(MemberScoreService.class);
	
	@Autowired
	private MemberScoreDao memberScoreDao;
	
	public MemberScore get(Long id) {
		return memberScoreDao.findOne(id);
	}
	
	public Page<MemberScore> find(Page<MemberScore> page, MemberScore memberScore) {
		DetachedCriteria dc = memberScoreDao.createDetachedCriteria();
		dc.createAlias("wechatMember", "wechatMember");
		if (StringUtils.isNotEmpty(memberScore.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+memberScore.getRemarks()+"%"));
		}
		
		/**
		 * 如果不是管理员只能看到自己的
		 */
		if(memberScore.getUser()!=null && memberScore.getUser().getId()>0  && !memberScore.getUser().isAdmin()){
			dc.add(Restrictions.eq("wechatMember.user.id", memberScore.getUser().getId()));
		}
		
		
		if(memberScore.getUser()!=null &&  !memberScore.getUser().isAdmin()){
			dc.add(Restrictions.eq("user.id", memberScore.getUser().getId()));
		}
		
		
		if (memberScore.getWechatMember()!=null && memberScore.getWechatMember().getId() != null){
			dc.add(Restrictions.eq("wechatMember.id", memberScore.getWechatMember().getId()));
		}
		
		if (memberScore.getWechatMember()!=null && !StringUtils.isEmpty(memberScore.getWechatMember().getMobile() )){
			dc.add(Restrictions.like("wechatMember.mobile", "%"+memberScore.getWechatMember().getMobile()+"%"));
		}
		
		if (memberScore.getWechatMember()!=null && !StringUtils.isEmpty(memberScore.getWechatMember().getName() )){
			dc.add(Restrictions.like("wechatMember.mobile", "%"+memberScore.getWechatMember().getName()+"%"));
		}
		

		if(memberScore.getCreateTime() !=null){
			dc.add(Restrictions.ge("createTime",memberScore.getCreateTime() ));
		}
		
		if(memberScore.getEndTime() !=null){
			dc.add(Restrictions.le("createTime",memberScore.getEndTime()));
		}
		
		//大于等的分数
		if (memberScore.getScore()!=null ){
			dc.add(Restrictions.ge("score", memberScore.getScore()));
		}
		
		dc.add(Restrictions.eq("delFlag", MemberScore.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return memberScoreDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(MemberScore memberScore) {
		memberScoreDao.save(memberScore);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		memberScoreDao.deleteById(id);
	}
	
}
